<template>
  <el-card>
    <div class="header-box">
      <!-- 商家 -->
      <div>
        <el-select
          v-model="queryInfo.provider_id"
          :loading="searchItem.provider_id.loading"
          :remote-method="handleSearchProvider"
          clearable
          filterable
          placeholder="请选择商家"
          remote
          reserve-keyword
          @change="handleResetPage('provider')"
        >
          <el-option
            v-for="item in searchItem.provider_id.options"
            :key="item.provider_id"
            :label="item.name + `(${item.account})`"
            :value="item.provider_id"
          >
          </el-option>
        </el-select>
      </div>
      <div>
        <el-select
          v-model="queryInfo.status"
          clearable
          placeholder="请选择状态"
          style="width: 180px"
          @change="handleResetPage"
        >
          <el-option
            v-for="(item, index) in filterItem.status"
            :key="index"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </div>
      <div>
        <el-button type="primary" @click="handleResetPage">
          搜索
          <i class="el-icon-search el-icon--right"></i>
        </el-button>
      </div>
      <div class="add">
        <el-button type="primary" @click="handleEdit()"> 对账</el-button>
      </div>
    </div>

    <div class="flex-content">
      <el-table
        v-loading="isLoading.table"
        :data="dataList"
        style="width: 100%"
      >
        <el-table-column
          align="center"
          label="商家"
          prop="provider"
          show-overflow-tooltip
        >
        </el-table-column>

        <el-table-column align="center" label="期次" prop="period">
        </el-table-column>

        <el-table-column align="center" label="对账单量">
          <template v-slot="{ row }">
            <el-button size="mini" type="primary" @click="handleOrder(row)">
              {{ row.order_settle_num }}
            </el-button>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          label="对账金额"
          prop="order_settle_fee"
        >
        </el-table-column>

        <el-table-column
          align="center"
          label="结算金额"
          prop="order_settle_commission"
        >
        </el-table-column>

        <el-table-column align="center" label="状态">
          <template v-slot="{ row }">
            <el-tag size="mini" type="primary">
              {{ row.status.trans }}
            </el-tag>
          </template>
        </el-table-column>

        <el-table-column align="center" label="对账时间" prop="create_time">
        </el-table-column>

        <el-table-column align="center" label="打款时间" prop="payment_time">
        </el-table-column>

        <el-table-column align="center" fixed="right" label="操作" width="300">
          <template v-slot="{ row }">
            <el-button
              v-if="row.status.origin === 0"
              size="mini"
              type="primary"
              @click="handlePay(row)"
            >
              打款
            </el-button>
            <el-button
              v-if="row.status.origin === 0"
              size="mini"
              type="danger"
              @click="handleRevoke(row)"
            >
              撤销
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页-->
    <div class="footer-box">
      <pagination
        :limit.sync="queryInfo.rows"
        :page.sync="queryInfo.page"
        :total="totalCount"
        @pagination="getData"
      >
      </pagination>
    </div>

    <!-- 添加与修改-->
    <Edit v-if="isDialog.edit" :info="dataInfo" @close="closeDialog"></Edit>
    <!-- 详情-->
    <Order v-if="isDialog.order" :info="dataInfo" @close="closeDialog"></Order>
  </el-card>
</template>

<script>
import { lists as getList, pay, revoke } from "@/api/reconciliation";
import Edit from "./components/edit.vue";
import Order from "./components/order.vue";
import { lists as searchProvider } from "@/api/provider";

export default {
  name: "Reconciliation",
  components: {
    Edit,
    Order,
  },
  data() {
    return {
      // 查询条件
      queryInfo: {
        page: 1,
        rows: 10,
        provider_id: "",
        status: "",
      },
      // 总页数
      totalCount: 0,
      // 列表
      dataList: [],
      // 详情
      dataInfo: {},
      // 筛选项
      filterItem: {
        status: [],
      },
      // 远程搜索项
      searchItem: {
        provider_id: {
          loading: false,
          options: [],
        },
      },
      // 加载
      isLoading: {
        table: false,
      },
      // 弹窗
      isDialog: {
        edit: false,
        order: false,
      },
      // 防抖
      queryId: +new Date(),
    };
  },
  created() {
    this.queryInfo.status = this.$route.query.status
      ? Number(this.$route.query.status)
      : "";
    this.handleSearchProvider();
    this.getData();
  },
  methods: {
    pay,
    // 列表
    getData() {
      this.isLoading.table = true;
      this.queryId++;
      let queryId = this.queryId;
      getList(this.queryInfo)
        .then((res) => {
          if (queryId !== this.queryId) {
            return;
          }
          this.dataList = res.data.list;
          this.totalCount = res.data.total;
          this.filterItem.status = res.data.status_screen;
          this.filterItem.refund = res.data.refund_screen;
          this.filterItem.pickup_method = res.data.pickup_method_screen;
        })
        .finally(() => {
          this.isLoading.table = false;
        });
    },
    // 编辑
    handleEdit(row) {
      this.dataInfo = row;
      this.isDialog.edit = true;
    },
    // 详情
    handleOrder(row) {
      this.dataInfo = row;
      this.isDialog.order = true;
    },
    // 撤销
    handleRevoke(row) {
      this.$confirm("此操作将撤销信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        callback: () => {},
        beforeClose: (action, ctx, close) => {
          if (action !== "confirm") {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
            close();
            return false;
          } else {
            ctx.confirmButtonLoading = true;
            revoke({
              r_id: row.r_id,
            })
              .then(() => {
                this.getData();
                close();
              })
              .finally(() => {
                ctx.confirmButtonLoading = false;
              });
          }
        },
      });
    },
    // 打款
    handlePay(row) {
      this.$confirm("此操作将打款对账, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        callback: () => {},
        beforeClose: (action, ctx, close) => {
          if (action !== "confirm") {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
            close();
            return false;
          } else {
            ctx.confirmButtonLoading = true;
            pay({
              r_id: row.r_id,
            })
              .then(() => {
                this.getData();
                close();
              })
              .finally(() => {
                ctx.confirmButtonLoading = false;
              });
          }
        },
      });
    },
    // 商家
    handleSearchProvider(query) {
      this.searchItem.provider_id.loading = true;
      searchProvider({
        page: 1,
        rows: 10,
        keywords: query ? query : "",
      }).then((res) => {
        this.searchItem.provider_id.loading = false;
        this.searchItem.provider_id.options = res.data.list;
      });
    },
    // 重置页面数据
    handleResetPage() {
      this.queryInfo.page = 1;
      this.getData();
    },
    //关闭
    closeDialog(val) {
      if (!val) {
        this.getData();
      }
      this.isDialog.edit = false;
      this.isDialog.order = false;
    },
  },
};
</script>

<style lang="scss" scoped></style>
